package Tread;

public class Example6_synchronized {
    private static final Object lock = new Object();
    private static boolean isProduced = false;

    public static void main(String[] args) {
        Thread product = new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println("product: Producing");
                    Thread.sleep(2000);
                    isProduced = true;
                    System.out.println("product is over");
                    lock.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread consumer = new Thread(() -> {
            synchronized (lock) {
                try {
                    while (!isProduced) {
                        System.out.println("Consumer is waiting for product's over");
                        lock.wait();
                    }
                    System.out.println("Consumer...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        consumer.start();
        product.start();
    }
}
